package site.lanmushan.quiz.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.lanmushan.common.authorization.CurrentUserUtil;
import site.lanmushan.common.mapper.wrapper.DefaultWrapper;
import site.lanmushan.common.mapper.wrapper.QueryWrapper;
import site.lanmushan.common.query.service.BaseService;
import site.lanmushan.quiz.api.ao.StartQuizTbBankExamAo;
import site.lanmushan.quiz.api.conts.QuizTbBankExamRecordStatusEnum;
import site.lanmushan.quiz.api.entity.QuizTbBankExamRecord;
import site.lanmushan.quiz.api.service.*;
import site.lanmushan.quiz.api.vo.QuizTbBankDescVo;
import site.lanmushan.quiz.mapper.QuizTbBankExamRecordMapper;
import org.springframework.transaction.annotation.Transactional;
import site.lanmushan.quiz.mapper.QuizTbQuestionAnswerRecordMapper;
import site.lanmushan.quiz.mapper.QuizTbQuestionMapper;

import java.util.List;

/**
 * @author dy
 */

@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class IQuizTbBankExamRecordServiceImpl extends BaseService<QuizTbBankExamRecord, QuizTbBankExamRecordMapper> implements IQuizTbBankExamRecordService {
    @Autowired
    private IQuizTbBankService quizTbBankService;
    @Autowired
    private QuizTbQuestionAnswerRecordMapper quizTbQuestionAnswerRecordMapper;
    @Autowired
    private QuizTbQuestionMapper quizTbQuestionMapper;
    @Autowired
    private IQuizTbQuestionAnswerService quizTbQuestionAnswerService;
    @Autowired
    private IQuizTbQuestionAnswerRecordService quizTbQuestionAnswerRecordService;

    @Override
    public QuizTbBankExamRecord startExam(StartQuizTbBankExamAo startQuizTbBankExamAo) {
        QueryWrapper queryWrapper = new DefaultWrapper();
        queryWrapper.eq("status", 0);
        queryWrapper.eq("create_user_account", CurrentUserUtil.getCurrentUser().getAccount());
        queryWrapper.eq("fk_bank_code", startQuizTbBankExamAo.getBankCode());
        List<QuizTbBankExamRecord> quizTbBankExamRecords = getQueryMapper().selectAllList(queryWrapper);
        if (quizTbBankExamRecords.size() > 0) {
            quizTbBankExamRecords.stream().forEach(it -> {
                it.setStatus(QuizTbBankExamRecordStatusEnum.EXPIRED.getStatus());
            });
            getQueryMapper().updateListByPrimaryKey(quizTbBankExamRecords);
        }
        QuizTbBankDescVo quizTbBankDescVo = quizTbBankService.selectBankInfoByBankCode(startQuizTbBankExamAo.getBankCode());
        QuizTbBankExamRecord quizTbBankExamRecord = new QuizTbBankExamRecord();
        quizTbBankExamRecord.setStatus(QuizTbBankExamRecordStatusEnum.ANSWERING.getStatus());
        quizTbBankExamRecord.setFkBankCode(startQuizTbBankExamAo.getBankCode());
        quizTbBankExamRecord.setFkBankName(quizTbBankDescVo.getBankName());
        quizTbBankExamRecord.setQuestionSum(quizTbBankDescVo.getQuestionSum());
        quizTbBankExamRecord.setQuestionAnswerCount(0);
        super.insertService(quizTbBankExamRecord);
        return quizTbBankExamRecord;
    }

    @Override
    public QuizTbBankExamRecord selectCurrentUserAnsweringExam(String bankCode) {
        QuizTbBankExamRecord query = new QuizTbBankExamRecord();
        query.setFkBankCode(bankCode);
        query.setStatus(QuizTbBankExamRecordStatusEnum.ANSWERING.getStatus());
        query.setCreateUserAccount(CurrentUserUtil.getCurrentUser().getAccount());
        return getQueryMapper().selectOne(query);
    }

}
